package com.campuscarbooking.api.listener;

import com.campuscarbooking.api.config.RabbitMQConfig;
import com.campuscarbooking.api.dto.message.ScheduleUpdateMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * Listener for schedule update messages from RabbitMQ
 */
@Component
@RequiredArgsConstructor
public class ScheduleUpdateListener {
    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ScheduleUpdateListener.class);

    @RabbitListener(queues = RabbitMQConfig.SCHEDULE_UPDATE_QUEUE)
    public void handleScheduleUpdate(ScheduleUpdateMessage message) {
        log.info("Received schedule update message: {}", message);

        try {
            // Handle different update types
            switch (message.getUpdateType()) {
                case "CREATED":
                    handleScheduleCreated(message);
                    break;
                case "UPDATED":
                    handleScheduleUpdated(message);
                    break;
                case "CANCELLED":
                    handleScheduleCancelled(message);
                    break;
                default:
                    log.warn("Unknown update type: {}", message.getUpdateType());
            }
        } catch (Exception e) {
            log.error("Error processing schedule update message", e);
        }
    }

    private void handleScheduleCreated(ScheduleUpdateMessage message) {
        log.info("Processing schedule created update for schedule ID: {}", message.getScheduleId());
        // In a real implementation, this could:
        // 1. Update dashboards
        // 2. Notify administrators
        // 3. Trigger other business processes like resource allocation
    }

    private void handleScheduleUpdated(ScheduleUpdateMessage message) {
        log.info("Processing schedule updated update for schedule ID: {}", message.getScheduleId());
        // Similar to above, handle update-specific logic
    }

    private void handleScheduleCancelled(ScheduleUpdateMessage message) {
        log.info("Processing schedule cancelled update for schedule ID: {}", message.getScheduleId());
        // Handle cancellation-specific logic, such as notifying affected users
    }
} 